import {
  getAllCategory,
  getThirdCategory,
  batchOfflineCategory,
  updateCategory,
  offlineCategory,
  upCategory,
  downCategory,
  addCategory,
  saveThirdSortCategory,
  notSaveThirdSortCategory,
  saveSortCategory,
  notSaveSortCategory,
} from '../../services/product/api';


export default {
  namespace: 'classifyIcon',

  state: {

    loading: true,

    allCategoryIconData: [],

    thirdCategoryIconData: [],

    groupList: [],

  },

  effects: {

    *fetchGetAllCategoryIcon({payload,callback}, { call, put }) {
      const response = yield call(getAllCategory, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { allCategoryIconData: response.data }
        });
      }
      if (callback && response) callback(response);
    },

    *fetchGetThirdCategory({payload,callback}, { call, put }) {
      const response = yield call(getThirdCategory, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { thirdCategoryIconData: response.data }
        });
      }
      if (callback && response) callback(response);
    },

    *fetchBatchOfflineCategory({payload,callback}, { call, put }) {
      const response = yield call(batchOfflineCategory, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { batchOfflineCategoryRes: response.data }
        });
      }
      if (callback && response) callback(response);
    },

    *fetchUpdateCategory({payload,callback}, { call, put }) {
      const response = yield call(updateCategory, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { updateCategoryRes: response.data }
        });
      }
      if (callback && response) callback(response);
    },

    *fetchOfflineCategory({payload,callback}, { call, put }) {
      const response = yield call(offlineCategory, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { offlineCategoryRes: response.data }
        });
      }
      if (callback && response) callback(response);
    },

    *fetchUpCategory({payload,callback}, { call, put }) {
      const response = yield call(upCategory, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { upCategoryRes: response.data }
        });
      }
      if (callback && response) callback(response);
    },

    *fetchDownCategory({payload,callback}, { call, put }) {
      const response = yield call(downCategory, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { downCategoryRes: response.data }
        });
      }
      if (callback && response) callback(response);
    },

    *fetchAddCategory({payload,callback}, { call, put }) {
      const response = yield call(addCategory, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { addCategoryRes: response.data }
        });
      }
      if (callback && response) callback(response);
    },

    *fetchSaveThirdSortCategory({payload,callback}, { call, put }) {
      const response = yield call(saveThirdSortCategory, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { saveThirdSortCategoryRes: response.data }
        });
      }
      if (callback && response) callback(response);
    },

    *fetchNotSaveThirdSortCategory({payload,callback}, { call, put }) {
      const response = yield call(notSaveThirdSortCategory, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { notSaveThirdSortCategoryRes: response.data }
        });
      }
      if (callback && response) callback(response);
    },

    *fetchSaveSortCategory({payload,callback}, { call, put }) {
      const response = yield call(saveSortCategory, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { saveSortCategoryRes: response.data }
        });
      }
      if (callback && response) callback(response);
    },

    *fetchNotSaveSortCategory({payload,callback}, { call, put }) {
      const response = yield call(notSaveSortCategory, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { notSaveSortCategoryRes: response.data }
        });
      }
      if (callback && response) callback(response);
    },

  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },

};
